﻿using Newtonsoft.Json;
using System.IO;
using System.IO.Pipes;
using System.Net;
using System.Text;
using TestWebApi.Entry;

namespace TestWebApi.Controllers
{
    public class TestPool
    {
        private static readonly string SAVEPATH = "D:\\BotFile\\CI\\";
        public static void Main(string[] args)
        {
            Dictionary<string, string> requstHead = new Dictionary<string, string>();
            string s = HttpRequest.SendHttp.SendGetByHttp("http://api.yujn.cn/api/dcd.php", requstHead);
            var data = JsonConvert.DeserializeObject<Img>(s);
            foreach (string imgurl in data.images)
            {
                Console.WriteLine(imgurl);
                ThreadPool.QueueUserWorkItem(ImgeUrlToPath, imgurl);
            }
        }
        //保存图片到本地
        public static async void ImgeUrlToPath(Object o)
        {
            var url = o.ToString();
            //io操作
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.GetAsync(url);
            Stream imageStream = await response.Content.ReadAsStreamAsync();

            StringBuilder imgPath = new StringBuilder();
            imgPath.Append(SAVEPATH).Append(Guid.NewGuid().ToString("N").Substring(0, 10)).Append(".jpg");

            using (FileStream fileStream = File.Create(imgPath.ToString()))
              {
                await imageStream.CopyToAsync(fileStream);
              }
            

        }
    }
}
